﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySite.Model;
using MySite.Helper;
using System.Xml;
using MySite.Enum;
using MySite.BLL;

namespace MySite.Timer
{
    public class CnBetaTimer : ITimer
    {
        public string cnBetaURL = System.Configuration.ConfigurationManager.AppSettings["cnBeta.com"];

        public static IList<BlogInfo> biList = new List<BlogInfo>();

        public override void GlobalTimer_ToDo(object state)
        {
            biList.Clear();
            string result =new HttpRequestHelper().HttpRequestGet(cnBetaURL);

            XmlHelper xml = new XmlHelper();
            xml.XmlHelperInitByXMLString(result);


            XmlNodeList xnl = xml.Read("item");

            foreach (XmlElement xn in xnl)
            {
                BlogInfo bi = new BlogInfo();
                bi.ID = xn.GetElementsByTagName("guid")[0].InnerText;
                bi.Content = xn.GetElementsByTagName("description")[0].InnerText.Replace("'","\"");
                bi.OriginalID = xn.GetElementsByTagName("guid")[0].InnerText;
                bi.OriginalURL = xn.GetElementsByTagName("guid")[0].InnerText;
                bi.PublishTime = Convert.ToDateTime(xn.GetElementsByTagName("pubDate")[0].InnerText);
                bi.Title = xn.GetElementsByTagName("title")[0].InnerText.Replace("'", "\"");
                bi.Summary = xn.GetElementsByTagName("category")[0].InnerText.Replace("'", "\"");
                bi.Type = BlogType.CnBeta;
                bi.Catalog = xn.GetElementsByTagName("category")[0].InnerText.Replace("'", "\"");
                biList.Add(bi);
            }
            new Blog().Delete(string.Format(" DateDiff(\"d\",PublishTime,Date())>0"));
            new Blog().UpdateBlog(biList);
        }
    }
}
